package com.jianghu;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.jianghu.dto.BaseDto;
import com.jianghu.dto.RecommendArticle;
import com.jianghu.helpers.ContentUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class RecommendActivity extends Activity {
	
	public Context context=RecommendActivity.this;
	ContentUtil handler = new ContentUtil(this);
	
	public String UserName;
	public static String CONTENT_PATH = "/article/rec_list";
	// recommend list view;
	private ListView listView;
	View loadingLayout;
	SimpleAdapter listItemAdapter;
	ArrayList<HashMap<String, Object>> listItem;

	TextView tv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recommendofone);
		
		tv=(TextView)findViewById(R.id.recommend_content);
		tv.setText(Html.fromHtml("就这样，十天时间过去了，重装步乓师团的战士们勉强压抑着对无双营的羡慕嫉妒恨，每天依旧保持训练。()除了吃喝以外，无双营的战士在训练闲暇之余还会和重半兵这边的战士聊天。<a href='#'>阅读全文</a>..."));
		
		
		ImageButton home = (ImageButton) findViewById(R.id.homebuttonofusertrends);
		home.setOnClickListener(new homeClickListener());
		ImageButton refresh = (ImageButton) findViewById(R.id.refreshbuttonofusertrends);
		refresh.setOnClickListener(new refreshClickListener());

		listView = (ListView) findViewById(R.id.listViewOfRecommend);
		loadingLayout = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_listview_user_trends, null, false);
		listView.addFooterView(loadingLayout, null, true);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.setClass(RecommendActivity.this, Article.class);

				RecommendActivity.this.startActivity(intent);
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();

		init();
		listItemAdapter = new SimpleAdapter(this, listItem,
				R.layout.listview_recommend_trends, new String[] { "title" }, new int[] {R.id.recommend_title });
		listView.setAdapter(listItemAdapter);
	}

	private void init() {
		Log.d("jianghu", "recommend list init...");
		listItem = new ArrayList<HashMap<String, Object>>();
		try {
			ContentUtil handler = new ContentUtil(this);
			String json = handler.getJson(CONTENT_PATH);
			Type typeOfT = new TypeToken<List<RecommendArticle>>() {
			}.getType();
			Gson gs = new Gson();
			List<RecommendArticle> list = gs.fromJson(json, typeOfT);
			for (RecommendArticle article : list) {
				HashMap<String, Object> map = BaseDto.getFieldMap(article,
						RecommendArticle.class);
				//map.put("timeoftrends", article.getCreateAtString());

				/*
				 * URL comeFromIcon = article.from_icon; Bitmap icon =
				 * BitmapFactory.decodeResource(getResources(),
				 * R.drawable.usericon); try{ icon =
				 * BitmapFactory.decodeStream(handler
				 * .getURLInputStream(comeFromIcon)); }catch(Exception e){
				 * Log.d("jianghu", comeFromIcon+" not found"); }
				 */
				map.put("title", "标题");
				//map.put("from_icon", R.raw.comments_icon);
				listItem.add(map);
			}
		} catch (JsonSyntaxException e) {
			Log.d("jianghu", e.getMessage(), e);
		} catch (IOException e) {
			Log.d("jianghu", e.getMessage(), e);
		}
	}

	class homeClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(RecommendActivity.this, MainPage.class);
			RecommendActivity.this.startActivity(intent);
		}

	}

	class refreshClickListener implements OnClickListener {
		private ProgressDialog progressDialog;

		@Override
		public void onClick(View v) {

			progressDialog = ProgressDialog.show(RecommendActivity.this,
					"刷新中..", "请稍等...", true, false);
			new Thread() {

				@Override
				public void run() {
					init();
					handlerRefresh.sendEmptyMessage(0);
				}
			}.start();
		}

		private Handler handlerRefresh = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				listView.setSelection(0);
				progressDialog.dismiss();
			}
		};
	}

	public class SpecialAdapter extends SimpleAdapter {

		public SpecialAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);

			//ImageView usericon = (ImageView) view.findViewById(R.id.comeFromIconinRecommend);
			//TextView txtUserName=(TextView) view.findViewById(R.id.recommendComeFrom);
			//final String UserName=txtUserName.getText().toString();
			//usericon.setOnClickListener(new View.OnClickListener() {
			// @Override
			// public void onClick(View arg0) {
			//
			// Intent intent = new Intent();
			// intent.setClass(context, UserTrendsManual.class);
			// Bundle bundle = new Bundle();
			// bundle.putString("item", UserName);
			// bundle.putString("type", "friends");
			// intent.putExtras(bundle);
			// context.startActivity(intent);
			// }
			// });

			return view;

		}
		
		/**
		 * 覆盖父类方法以通过一个url读取本地缓存的图片
		 */
		public void setViewImage(ImageView v, String value) {
			Log.d("jianghu",value);
			try {
				InputStream is = handler.getURLInputStream(value);
				Bitmap bitmap = BitmapFactory.decodeStream(is);
				v.setImageBitmap(bitmap);
			} catch (IOException e) {
				Log.d("jianghu", "can not open inputStream path:"+value);
				v.setImageResource(R.drawable.default_user_icon);
			}
		}

		public void setViewImage(ImageView v, int res) {
			Log.d("jianghu", res+"");
			super.setViewImage(v, res);
		}
	}
}
